package com.sny.upgrade;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.sny.upgrade.firebase.config.IFireBaseConfigObserver;
import com.sny.upgrade.ui.DialogShow;
import com.sny.upgrade.ui.UpdateConfirmDialog;
import com.sny.upgrade.util.SPUtils;

/**
 * Created by Admin on 2017/6/6.
 */

public class AppUpgradeImple implements IAppUpgrade, IFireBaseConfigObserver {

    private Context mContext;
    //这个地方也不有问题呀。TODO
    private Activity mHomeActivity;
    public static final String LAST_NOTIFY_TIME = "last_notify_time";
    private static final long DURATION = 1 * 24 * 3600 * 1000;
    public static final String CUR_CHECK_FRU = "cur_check_fru";
    private long mInterval = 24 * 3600 * 1;

    public AppUpgradeImple(Context context, Activity homeActivity) {
        this.mContext = context;
        this.mHomeActivity = homeActivity;
    }

    public AppUpgradeImple(Context context, Activity homeActivity, long interval) {
        this.mContext = context;
        this.mHomeActivity = homeActivity;
    }

    private boolean isNeedUpgrade(Context context) {

        boolean result = false;
        int versionCode = -1;
        try {

            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = pi.versionCode;

        } catch (PackageManager.NameNotFoundException ex) {
            ex.printStackTrace();
            //当出现异常时，不升级
            result = false;
        }

        FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        //获取当前最新的版本号
        long cur_version = mFirebaseRemoteConfig.getLong("cur_version_code");


        if (cur_version > versionCode && versionCode != -1) {
            result = true;
        } else {
            result = false;
        }

        Log.i("upgrade", "is need upgrade :\t" + result + "\nversoin in service is " + cur_version + "\n" + "versioncode:" + versionCode);
        return result;
    }

    @Override
    public void checkUpgrade(Context context, Activity activity) {

        boolean result = isNeedUpgrade(context);

        if (result) {
            UpdateConfirmDialog updateConfirmDialog = new UpdateConfirmDialog(activity, false);
            DialogShow dialogShow = new DialogShow(updateConfirmDialog, activity, activity);
            dialogShow.run();
        }
    }

    @Override
    public void onConfigFetchComplete(@NonNull Task<Void> task) {

        if (filter()) {
            checkUpgrade(mContext, mHomeActivity);
        }

    }


    @Override
    public boolean filter() {

        long fru = FirebaseRemoteConfig.getInstance().getLong(CUR_CHECK_FRU);
        long duration = fru * DURATION;

        boolean result = false;
        //超过了设计的时间间隔才提醒
        long last_notify_time = (long) SPUtils.get(mContext, LAST_NOTIFY_TIME, 0L);

        if ((System.currentTimeMillis() - last_notify_time) > duration || last_notify_time == 0) {
            result = true;
        } else {
            result = false;
        }
        if (BuildConfig.DEBUG) {
            Log.i("upgrade", "filter result :\t" + result + "\tlast_notify_time\t" + last_notify_time + "\t fru :\t" + fru);
        }

        return result;
    }
}
